package com.cc.crm.controller;

import com.cc.crm.pojo.Customer;
import com.cc.crm.service.CustomerService;
import com.cc.crm.vo.CustomerVo;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;

    //跳转到CRM页面
    @RequestMapping("/toCrm")
    public String toCrm() {
        return "crm";
    }

    //查询属于用户所有客户
    @RequestMapping("/selectCustomer")
    @ResponseBody
    public PageInfo<Customer> selectCustomer(CustomerVo customerVo){
        PageInfo<Customer> customerList = customerService.selectCustomer(customerVo);
        return customerList;
    }
    
    //修改客户信息
    @RequestMapping("/updateCustomer")
    @ResponseBody
    public int updateCustomer(Customer customer) {
        int i = customerService.updateCustomer(customer);
        return i;
    }
    
    //添加客户信息
    @RequestMapping("/insertCustomer")
    @ResponseBody
    public int insertCustomer(@Valid Customer customer,BindingResult result) {
        if (result.hasErrors()) {
            //如果有验证错误，处理错误信息
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError error : errors) {
                System.out.println("出错的属性是：" + error.getField() + "提示信息：" + error.getDefaultMessage());
            }
            return 0;
        }
        int i = customerService.insertCustomer(customer);
        return i;
    }
    
    //删除客户信息
    @RequestMapping("/deleteCustomer")
    @ResponseBody
    public int deleteCustomer(Customer customer) {
        int i = customerService.deleteCustomer(customer);
        return i;
    }
    
}
